package cn.aj.pet.controller;

import cn.aj.pet.service.IPetTypeService;
import cn.aj.pet.domain.PetType;
import cn.aj.pet.query.PetTypeQuery;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
public class PetTypeController {
    @Autowired
    public IPetTypeService petTypeService;


    /**
     * 保存和修改公用的
     * @param petType  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody PetType petType){
        try {
            if( petType.getId()!=null)
                petTypeService.update(petType);
            else
                petTypeService.add(petType);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            petTypeService.delete(id);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public PetType get(@PathVariable("id")Long id)
    {
        return petTypeService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<PetType> list(){

        return petTypeService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
     * tree
    */
    @PostMapping("/list")
    public PageListUtil<PetType> json(@RequestBody PetTypeQuery query)
    {
        return petTypeService.loadPage(query);
    }

    /**
    * @Title: tree
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/3 0003 17:35
    * @Parameters: []
    * @Return java.util.List<cn.aj.pet.domain.PetType>
     *     类型无限极树
    */
    @GetMapping("/tree")
    public List<PetType> tree()
    {
        return petTypeService.tree();
    }
}
